/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.service.impl;

import com.minierp.bean.LoanBillBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDetailDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.LoanBillDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.DataChangedException;
import com.minierp.err.NotAllowException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.LoanBill;
import com.minierp.model.SystemLog;
import com.minierp.service.LoanBillService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;

public class LoanBillServiceImpl implements LoanBillService {
	public void txInsert(LoanBillBean form, LoanBillDao loanBillDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator,
			BankAccountBalanceDao bankAccountBalanceDao) throws Exception {
		LoanBill obj = form.getLoanBill();
		obj.setCreateTime(new Date());
		obj.setId(idGenerator.getId(LoanBill.class.getName()));
		if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
			obj.setBillCode(PageHelper.generateCode(serialNumberDao,
					form.getCodeRule(), "", form.getLoanBill().getTallySetId()));

			for (LoanBill billTypeName = loanBillDao.get(obj.getBillCode()); billTypeName != null; billTypeName = loanBillDao
					.get(obj.getBillCode())) {
				obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
						.getCodeRule(), "", form.getLoanBill().getTallySetId()));
			}
		}

		if (obj.getExamined().equals("Y")) {
			obj.setExamSms("N");
			obj.setExamineTime(new Date());
		} else {
			obj.setExamSms("Y");
		}

		loanBillDao.save(obj);
		if (obj.getExamined().equals("Y")) {
			String billTypeName1 = "借出";
			String financeType = "LOAN_OUT";
			String rcType = "P";
			if (obj.getBillType().equals("IN")) {
				billTypeName1 = "借入";
				rcType = "R";
				financeType = "LOAN_IN";
			}

			PageHelper.updateBankAccount(obj.getBankAccountId(),
					obj.getTallySetId(), obj.getId(), obj.getCost(),
					obj.getLoanDate(), obj.getExamineTime(), obj.getBillCode(),
					"LOAN_BILL", "", billTypeName1, rcType,
					obj.getManagerName(), obj.getMark(), financeType,
					"SAVE_EXAM", false, (BankAccountDetailDao) null,
					bankAccountBalanceDao, idGenerator);
		}

	}

	public void txUpdate(LoanBillBean form, LoanBillDao loanBillDao,
			IdGenerator idGenerator, BankAccountBalanceDao bankAccountBalanceDao)
			throws Exception {
		LoanBill obj = form.getLoanBill();
		Object[] sv = loanBillDao.getStatusValues(obj.getId());
		if (sv == null) {
			throw new Exception("Can\'t get status values for loan bill:"
					+ obj.getId());
		} else {
			long existSycVal = ((Date) sv[0]).getTime();
			String exitEnabled = sv[1].toString();
			String existExamined = sv[2].toString();
			String billName;
			if (obj.getBillType().equals("IN")) {
				billName = "资金借入单";
			} else {
				billName = "资金借出单";
			}

			if (existSycVal != form.getSychronizeValue()) {
				throw new DataChangedException(billName
						+ "已被其他操作修改，请关闭页面再打开后重新操作！");
			} else if (exitEnabled.equals("N")) {
				throw new NotAllowException(billName + "已作废，不能修改！");
			} else if (existExamined.equals("Y")) {
				throw new NotAllowException(billName + "已审核，不能修改！");
			} else {
				if (obj.getExamined().equals("Y")) {
					obj.setExamSms("N");
					obj.setExamineTime(new Date());
				} else {
					obj.setExamSms("Y");
				}

				obj.setUpdateTime(new Date());
				loanBillDao.update(obj);
				if (obj.getExamined().equals("Y")) {
					String billTypeName = "借出";
					String financeType = "LOAN_OUT";
					String rcType = "P";
					if (obj.getBillType().equals("IN")) {
						billTypeName = "借入";
						financeType = "LOAN_IN";
						rcType = "R";
					}

					PageHelper.updateBankAccount(obj.getBankAccountId(),
							obj.getTallySetId(), obj.getId(), obj.getCost(),
							obj.getLoanDate(), obj.getExamineTime(),
							obj.getBillCode(), "LOAN_BILL", "", billTypeName,
							rcType, obj.getManagerName(), obj.getMark(),
							financeType, "SAVE_EXAM", false,
							(BankAccountDetailDao) null, bankAccountBalanceDao,
							idGenerator);
				}

			}
		}
	}

	public void txExamine(HttpServletRequest request, long id,
			LoanBillDao loanBillDao, long examinerId, String examinerName,
			BankAccountBalanceDao bankAccountBalanceDao, IdGenerator idGenerator)
			throws Exception {
		LoanBill a = (LoanBill) loanBillDao.get(LoanBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("N")) {
			a.setExamined("Y");
			a.setExamSms("N");
			a.setExamineTime(new Date());
			a.setExaminerId(examinerId);
			a.setExaminerName(examinerName);
			a.setUpdateTime(new Date());
			loanBillDao.update(a);
			String billTypeName = "借出";
			String rcType = "P";
			String priCode = "LOAN_OUT_EXAM";
			String financeType = "LOAN_OUT";
			if (a.getBillType().equals("IN")) {
				billTypeName = "借入";
				rcType = "R";
				priCode = "LOAN_IN_EXAM";
				financeType = "LOAN_IN";
			}

			if (!EncCommonHelper.checkPrivilege(request, priCode)) {
				throw new Exception("Permission deny!");
			}

			PageHelper.updateBankAccount(a.getBankAccountId(),
					a.getTallySetId(), a.getId(), a.getCost(), a.getLoanDate(),
					a.getExamineTime(), a.getBillCode(), "LOAN_BILL", "",
					billTypeName, rcType, a.getManagerName(), a.getMark(),
					financeType, "EXAM", false, (BankAccountDetailDao) null,
					bankAccountBalanceDao, idGenerator);
		}

	}

	public void txUnExamine(HttpServletRequest request, long id,
			LoanBillDao loanBillDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao, IdGenerator idGenerator,
			UserBean ub) throws Exception {
		LoanBill a = (LoanBill) loanBillDao.get(LoanBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("Y")) {
			a.setExamined("N");
			a.setExamSms("Y");
			a.setExaminerId(0L);
			a.setExaminerName("");
			a.setUpdateTime(new Date());
			loanBillDao.update(a);
			String billTypeName = "借出";
			String financeType = "LOAN_OUT";
			String rcType = "P";
			String priCode = "LOAN_OUT_UN_EXAM";
			if (a.getBillType().equals("IN")) {
				billTypeName = "借入";
				financeType = "LOAN_IN";
				rcType = "R";
				priCode = "LOAN_IN_UN_EXAM";
			}

			if (!EncCommonHelper.checkPrivilege(request, priCode)) {
				throw new Exception("Permission deny!");
			}

			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String mark = ub.getStaffer().getStafferName() + "于"
					+ sf.format(new Date()) + "反审核单据";
			PageHelper.updateBankAccount(a.getBankAccountId(),
					a.getTallySetId(), a.getId(),
					(new BigDecimal(0)).subtract(a.getCost()), a.getLoanDate(),
					new Date(), a.getBillCode(), "LOAN_BILL", "", billTypeName,
					rcType, a.getManagerName(), mark, financeType, "UN_EXAM",
					true, bankAccountDetailDao, bankAccountBalanceDao,
					idGenerator);
		}

	}

	public void txCancel(HttpServletRequest request, long id, String reason,
			LoanBillDao loanBillDao, UserBean ub,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao, IdGenerator idGenerator)
			throws Exception {
		LoanBill d = (LoanBill) loanBillDao.get(LoanBill.class, id);
		if (d != null && d.getEnabled().equals("Y")) {
			d.setEnabled("N");
			d.setExamSms("N");
			d.setDisableManId(ub.getUser().getStafferId());
			d.setUpdateTime(new Date());
			loanBillDao.update(d);
			String billTypeName = "借出";
			String financeType = "LOAN_OUT";
			String rcType = "P";
			String priCode = "LOAN_OUT_CANC";
			String logActionType = "LOAN_OUT";
			if (d.getBillType().equals("IN")) {
				billTypeName = "借入";
				financeType = "LOAN_IN";
				rcType = "R";
				priCode = "LOAN_IN_CANC";
				logActionType = "LOAN_IN";
			}

			if (!EncCommonHelper.checkPrivilege(request, priCode)) {
				throw new Exception("Permission deny!");
			}

			if (d.getExamined().equals("Y")) {
				SimpleDateFormat log = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String mark = ub.getStaffer().getStafferName() + "于"
						+ log.format(new Date()) + "作废单据";
				PageHelper.updateBankAccount(d.getBankAccountId(),
						d.getTallySetId(), d.getId(),
						(new BigDecimal(0)).subtract(d.getCost()),
						d.getLoanDate(), new Date(), d.getBillCode(),
						"LOAN_BILL", "", billTypeName, rcType,
						d.getManagerName(), mark, financeType, "CANC", true,
						bankAccountDetailDao, bankAccountBalanceDao,
						idGenerator);
			}

			SystemLog log1 = new SystemLog();
			log1.setId(idGenerator.getId(SystemLog.class.getName()));
			log1.setActionCode(logActionType);
			log1.setLogType("CANCEL_LOG");
			log1.setCreaterId(ub.getStaffer().getId());
			log1.setCreaterName(ub.getStaffer().getStafferName());
			log1.setDepName(ub.getDepName());
			log1.setDepId(ub.getStaffer().getDepartmentId());
			log1.setStatus("S");
			log1.setUserName(ub.getUser().getUserName());
			log1.setIp(d.getBillCode());
			log1.setMark("");
			log1.setRelId(id);
			log1.setCancelReason(reason);
			log1.setTallySetId(d.getTallySetId());
			loanBillDao.save(log1);
		}

	}
}